module.exports = function($scope, $resource, toaster, getDate, $modal, str2date){

    $scope.bigCurrentPage = 1;
    $scope.itemsPerPage = 10;
    $scope.searchform = {'moreSearch': false};
    $scope.dictdata = {
        'pay1': '支付宝',
        'pay2': '微信支付',
        'pay3': '网银在线',
        'pay4': '平台余额',
        'pay5': '现金支付',
        'pay6': '供应商预存',
        'mail0': '未发货',
        'mail1': '备货中',
        'mail2': '已发货',
        'mail3': '已签收',    
    }
    $scope.state_list = [
        {'label':'--全部--', 'value':''},
        {'label':'未发货', 'value':'0'}, 
        {'label':'备货中', 'value':'1'},
        {'label':'已发货', 'value':'2'},
        {'label':'已签收', 'value':'3'}
    ];
    $scope.datestart = {
        'lable': null,
        'opened': false
    }
    $scope.dateend = {
        'lable': null,
        'opened': false
    }

    $scope.dateOpen = function($event, item){
        $event.preventDefault();
        $event.stopPropagation();
        item.opened = true;
    };

    $scope.loadData = function(pageNo){
        var para = {
            'pageNo': pageNo,
            'pageSize': $scope.itemsPerPage,
            'code': $scope.searchform.code,
            'start_time': getDate($scope.datestart.lable),
            'end_time': getDate($scope.dateend.lable)
        }
        if($scope.searchform.moreSearch){
            para.mail_state = $scope.searchform.state;
            para.title = $scope.searchform.title;
        }
        if($scope.searchform.person){
            if($scope.searchform.person.length == 11 && /^[0-9]*$/.test($scope.searchform.person)){
                para.mobile = $scope.searchform.person;
            } else {
                para.name = $scope.searchform.person;
            }
        }
        $resource('/api/ac/tc/ticketOrderService/entityOrderList', {}, {}).save(para, function(res){
            if (res.errcode === 0) {
                $scope.order_list = res.data.results;
                $scope.bigTotalItems = res.data.totalRecord;
                $scope.bigCurrentPage = pageNo;
            } else {
                toaster.error({title: '', body: '订单列表加载失败'});
            }
        });
    }
    $scope.loadData(1);

    $scope.orderinfo = function(obj){
        var modalInstance = $modal.open({
            template: require('../views/entityOrderInfo.html'),
            controller: 'entityOrderInfo',
            size: 'lg',
            resolve: {
                'obj' : function(){
                    return obj;
                },
                'dictdata' : function(){
                    return $scope.dictdata;
                }
            }
        });
    }

    //商客订单查看分润信息
    $scope.skv4Profit = function(obj){
        var para = {
            'order_code': obj.code
        }
        var modalInstance = $modal.open({
            template: require('../../55skwx/views/skwxShopOrderProfit.html'),
            controller: 'skwxShopOrderProfit',
            size: 'lg',
            resolve: {
                bean: function () {
                    return para;
                }
            }
        });
    }

    //确认发货|更改快递单(flag=true)，修改收货地址(flag=false)
    $scope.setOrderInfo = function(obj,flag){
        var modalInstance = $modal.open({
            template: require('../views/setOrderInfo.html'),
            controller: 'setOrderInfo',
            size: 'lg',
            resolve: {
                'obj' : function(){
                    return obj;
                },
                'updateAddress' : function(){
                    return flag;
                }
            }
        });
        modalInstance.result.then(function(result){
            obj.mail_state = result.mail_state || obj.mail_state;
            obj.courier_company_code = result.courier_company_code || obj.courier_company_code;
            obj.express_number = result.express_number || obj.express_number;
            obj.mail_name = result.mail_name || obj.mail_name;
            obj.mail_mobile = result.mail_mobile || obj.mail_mobile;
            obj.mail_address = result.mail_address || obj.mail_address;
        });
    }

    $scope.cancelSend = function(obj){
        if(confirm('取消发货，将删除已发货快递信息！')){
            var para = {
                'order_code': obj.code,
                'mail_state': '1',
            }
            $resource('/api/ac/tc/ticketOrderService/setExpressNumber', {}, {}).save(para, function(res){
                if (res.errcode === 0 && res.data == '1') {
                    toaster.success({title: '', body: '取消发货成功'});
                    obj.mail_state = '1';
                    obj.courier_company_code = '';
                    obj.express_number = '';
                } else {
                    toaster.error({title: '', body: '失败，请重试'});
                }
            });
        }
    }

    $scope.gotoKD = function(obj){
        if(!obj.express_number){
            toaster.error({title: '', body: '发送未填写快递单号，查询物流'});
            return;
        }
        window.open('https://www.kuaidi100.com/?coname=indexall&nu=' + obj.express_number, "_blank");
    }



    //----以下是页面快捷选择日期按钮-----
    $scope.today = function(){
        var now = new Date();
        $scope.datestart.lable = getDate(now);
        $scope.dateend.lable = $scope.datestart.lable;
    }

    $scope.yesterday = function(){
        var now = new Date();
        now.setDate(now.getDate() - 1);
        $scope.datestart.lable = getDate(now);
        $scope.dateend.lable = getDate(now);
    }

    $scope.thisWeek = function(){
        var now = new Date();
        $scope.dateend.lable = getDate(now);

        var week = now.getDay() - 1;
        week = week == -1 ? 6 : week
        now.setDate(now.getDate() - week);
        $scope.datestart.lable = getDate(now);
    }

    $scope.lastWeek = function(){
        var now = new Date();
        var week = now.getDay() - 1;
        week = week == -1 ? 6 : week
        now.setDate(now.getDate() - week);
        $scope.dateend.lable = getDate(now);

        now.setDate(now.getDate() - 7);
        $scope.datestart.lable = getDate(now);
    }

    $scope.thisMonth = function(){
        var now = new Date();
        $scope.dateend.lable = getDate(now);
        
        now.setDate(1);
        $scope.datestart.lable = getDate(now);
    }

    $scope.lastMonth = function(){
        var now = new Date();
        now.setDate(0);
        $scope.dateend.lable = getDate(now);

        now.setDate(1);
        $scope.datestart.lable = getDate(now);
    }


};